{extend name="public/base"}

{block name="x-body"}
    <div class="x-body">
        <form class="layui-form layui-form-pane">
            {if $id}
            <input type="hidden" name="id" value="{$id}">
            {/if}
            <div class="layui-form-item layui-form-text">
                <label class="layui-form-label">
                    用户名

                </label>
                <table  class="layui-table layui-input-block">
                    <tbody>

                        <tr class="current">
                            <td width="50">
                                <div class="layui-input-block">
                                    <input name="all" lay-skin="primary" type="checkbox" class="all" lay-filter="all" title="全选">
                                </div>
                            </td>
                            <td>
                                <div class="layui-input-block">
                                    {volist name="users" id="v"}
                                        <input name="users_id[]" lay-skin="primary" type="checkbox" class="cityId" lay-filter="c_one" title="{$v.username}{$v.checkbox ? '(已发放)' : ''}" value="{$v.id}">
                                    {/volist}
                                </div>
                            </td>
                        </tr>

                    </tbody>
                </table>
            </div>
            <div class="layui-form-item">
                <button class="layui-btn" lay-submit="" lay-filter="add">发放优惠券</button>
            </div>
        </form>
    </div>
    <script>
        formData('{:url("promotion/coupon_send")}', 'add', function (){
            parent.location.reload();
            // 获得frame索引
            var index = parent.layer.getFrameIndex(window.name);
            //关闭当前frame
            parent.layer.close(index);
        }, false);


        $(function (){
            layui.use('form', function() {
                var form = layui.form;

                //全选
                form.on('checkbox(all)', function (data) {
                    var child = $(data.elem).parents('tr').find('input[type="checkbox"]');
                    child.each(function (index, item) {
                        item.checked = data.elem.checked;
                    });
                    form.render('checkbox');
                });


                form.on('checkbox(c_one)', function (data) {
                    var item = $(".cityId").length;
                    let all = item;
                    for(var n = 0; n < item; n++){
                        if ($(".cityId").eq(n).prop("checked") == false) {
                            $(".all").prop("checked", false);
                            form.render('checkbox');
                            break;
                        }

                        if($(".cityId").eq(n).prop("checked") == true){
                            all--;
                        }

                        if (all == 0) {
                            $(".all").prop("checked", true);;
                            form.render('checkbox');
                        }
                    }
                });
            })
        })
    </script>
{/block}